/**
 * @Author: Jackey 327156719@qq.com
 * @Date: 2025-06-25 15:26:06
 * @LastEditors: Jackey 327156719@qq.com
 * @LastEditTime: 2025-06-25 17:20:19
 * @FilePath: src/main/java/org/springblade/common/utils/XxlJobUtil.java
 * @Description: 这是默认设置,可以在设置》工具》File Description中进行配置
 */
package org.springblade.common.utils;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class XxlJobUtil {

    // 此处的name 是xxl-job 存入cookie时设置的名字 必须一致
    private static final String cookieName = "XXL_JOB_LOGIN_IDENTITY";

    private static String getCookie() {
        return login();
    }

    /**
     * 登录方法
     * 可以将获取到的cookie缓存
     * @return cookie
     */
    private static String login() {
        String url = "http://job.sida-tech.com/xxl-job-admin/login";
        String userName = "sida";
        String password = "sida";
        try (
                HttpResponse response = HttpUtil.createPost(url)
                        .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                        .form("userName", userName)
                        .form("password", password).execute();
                ){

            if (response.getStatus() != 200) {
                throw new RuntimeException("登录xxl-job失败!网络请求异常,状态码：" + response.getStatus());
            }

            String setCookie = response.header("Set-Cookie");
            // 提取 XXL_JOB_LOGIN_IDENTITY cookie
            if (setCookie != null) {

                String[] cookies = setCookie.split(";");

                for (String cookie : cookies) {
                    if (cookie != null && cookie.trim().startsWith(cookieName)) {
                        return cookie.split("=")[1];
                    }
                }
            }

            throw new RuntimeException("登录xxl-job失败,未获取到cookie");

        } catch (Exception e) {
           throw new RuntimeException("登录xxl-job失败," + e.getMessage());
        }
    }


	/**
	 * 更新定时任务
	 * @param id 任务ID
	 * @param jobGroup 任务组ID
	 * @param jobDesc 任务描述
	 * @param author 任务作者
	 * @param scheduleType 调度类型
	 * @param scheduleConf Cron
	 * @param executorRouteStrategy 路由策略
	 * @param misfireStrategy 调度过期策略
	 * @param executorBlockStrategy 阻塞策略
	 * @return
	 */
    public static boolean update(Integer id,Integer jobGroup,String jobDesc,String author,String scheduleType, String scheduleConf,String executorRouteStrategy,String misfireStrategy,String executorBlockStrategy) {
        String url = "http://job.sida-tech.com/xxl-job-admin/jobinfo/update";
        try (
                HttpResponse response = HttpUtil.createPost(url)
                        .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                        .header("Cookie",String.format("%s=%s", cookieName, getCookie()))
                        .form("id",id)
						.form("jobGroup",jobGroup)
						.form("jobDesc",jobDesc)
						.form("author",author)
						.form("scheduleType",scheduleType)
						.form("scheduleConf",scheduleConf)
						.form("executorRouteStrategy",executorRouteStrategy)
						.form("misfireStrategy",misfireStrategy)
						.form("executorBlockStrategy",executorBlockStrategy).execute()
        ) {
            if (response.getStatus() == 200) {
                JSONObject body = JSON.parseObject(response.body());
                Integer code = body.getInteger("code");
                if (code == 200) {
                    // 调度成功
                    return true;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("定时任务调度失败," + e.getMessage());
        }
        return false;
    }



}
